package com.elitel.common;

import com.elitel.common.Base64Util;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

public class DESUtil {
    private static final String DES_ALGORITHM = "DES";
    private static final String CHARSET = "utf-8";

    /**
     * DES加密
     *
     * @param plainData 原始字符串
     * @param secretKey 加密密钥
     * @return 加密后的字符串
     * @throws Exception
     */
    public static String encryption(String plainData, String secretKey) throws Exception {

        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(DES_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, generateKey(secretKey));

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {

        }

        try {
            // 为了防止解密时报javax.crypto.IllegalBlockSizeException: Input length must
            // be multiple of 8 when decrypting with padded cipher异常，
            // 不能把加密后的字节数组直接转换成字符串
            byte[] buf = cipher.doFinal(plainData.getBytes());

            return Base64Util.encode(buf);

        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            throw new Exception("IllegalBlockSizeException", e);
        } catch (BadPaddingException e) {
            e.printStackTrace();
            throw new Exception("BadPaddingException", e);
        }
    }


    /**
     * DES解密
     *
     * @param secretData 密码字符串
     * @param secretKey  解密密钥
     * @return 原始字符串
     * @throws Exception
     */
    public static String decryption(String secretData, String secretKey) {

        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(DES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey));

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
//            String s = Base64Util.baseConvertStr(secretData);
//            return s;
            byte[] buf = cipher.doFinal(Base64Util.decode(secretData.toCharArray()));
            return new String(buf,CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";

    }

    /**
     * 获得秘密密钥
     *
     * @param secretKey
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws InvalidKeyException
     */
    private static SecretKey generateKey(String secretKey)
            throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, UnsupportedEncodingException {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
        DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes(CHARSET));
        keyFactory.generateSecret(keySpec);
        return keyFactory.generateSecret(keySpec);
    }

//    public static void main(String[] args) {
//        String des = "0x0XUe1JKd4rl7g/0JfOvq/zfNKd9FcGBIFKWf/6iIBkqp3zKWNI6sED9+/ADORp+vVggSnPMOW5cAB7p2shcay+9wI7L9kzebUVIu9hAGlL3sM6CmsADaITLMWWdLgVfBFZ7KetdCI=";
//        String decryption = decryption(des, "12345678");
//        System.out.println(decryption);
//    }
}
